<?php

namespace App\Models;

class Words extends Base
{
    protected $table = 'words';

    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">锁定</span>',
    ];

    const HOT_1 = 1;
    const HOT_2 = 2;
    const HOT_TEXT = [
        self::HOT_1 => '<span class="el-tag">否</span>',
        self::HOT_2 => '<span class="el-tag  el-tag--danger">是</span>',
    ];

    const GOOD_1 = 1;
    const GOOD_2 = 2;
    const GOOD_TEXT = [
        self::GOOD_1 => '<span class="el-tag">否</span>',
        self::GOOD_2 => '<span class="el-tag  el-tag--danger">是</span>',
    ];

    protected $fillable = [
        'id','title','thumb','cate_pid','cate_id','summary','view','download','collect','format',
        'size','source','file_link','show_link','hot','good','status','created_at','updated_at'
    ];

    public function cate_pid(){//一级分类
        return $this->belongsTo('App\Models\WordCategorys', 'cate_pid', 'id');
    }
    public function cate_id(){//二级分类
        return $this->belongsTo('App\Models\WordCategorys', 'cate_id', 'id');
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('title','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['hot']) && $params['hot']) {
                    $query ->where('hot',$params['hot']);
                }
                if(isset($params['good']) && $params['good']) {
                    $query ->where('good',$params['good']);
                }
                if(isset($params['cid']) && $params['cid']) {
                    $query ->where('cate_id',$params['cid'])->orwhere('cate_pid',$params['cid']);
                }
                if(isset($params['start']) && $params['start']) {
                    $query ->whereDate('created_at','>=',$params['start']);
                }
                if(isset($params['end']) && $params['end']) {
                    $query ->whereDate('created_at','<=',$params['end']);
                }
            })->where('status','<>',-1)
            ->with(['cate_pid' => function ($query) {
                $query->select(['id','name'])->get();
            }])->with(['cate_id' => function ($query) {
                $query->select(['id','name'])->get();
            }])->when($params, function ($query) use($params){
                if(isset($params['sort']) && $params['sort']) {
                    return $query->orderBy('download',$params['sort']);
                }
            })->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
            'hot'   => self::HOT_TEXT,
            'good'  => self::GOOD_TEXT,
        ]);
        return $lists;
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = '', $limit = 10, $sort = 'created_at', $orderby = 'desc'){
        $params['status'] = 1;
        if(isset($page)) {
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->where('id',$params['id']);
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                    if(isset($params['hot']) && $params['hot']) {
                        $query ->where('hot',$params['hot']);
                    }
                    if(isset($params['good']) && $params['good']) {
                        $query ->where('good',$params['good']);
                    }
                    if(isset($params['cid']) && $params['cid']) {
                        $query ->where('cate_pid',$params['cid']);
                    }
                    if(isset($params['cid_pid']) && $params['cid_pid']) {
                        $query ->whereIn('cate_pid',$params['cid_pid']);
                    }
                    if(isset($params['cid_id']) && $params['cid_id']) {
                        $query ->whereIn('cate_id',$params['cid_id']);
                    }
                    if(isset($params['keyword']) && $params['keyword']) {
                        $keyword = $params['keyword'];
                        $query ->where("title", "like", "%$keyword%");
                    }
                })->where('status','<>',-1)
                ->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        }else {
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->whereIn('id',$params['id']);
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                    if(isset($params['hot']) && $params['hot']) {
                        $query ->where('hot',$params['hot']);
                    }
                    if(isset($params['good']) && $params['good']) {
                        $query ->where('good',$params['good']);
                    }
                    if(isset($params['cid']) && $params['cid']) {
                        $query ->where('cate_id',$params['cid'])->orwhere('cate_pid',$params['cid']);
                    }
                })->where('status','<>',-1)
                ->orderBy($sort,'desc')->limit($limit)->get();
        }
        return $lists;
    }

    /**
     * 根据id查询一条文档
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function details($id, $fields = ['*']){
        return $this->select($fields)->where($id)->where('status','<>',-1)->first();
    }


}
